package com.fancy.mobileschool.ui.adapt;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fancy.mobileschool.MyConfiguration;
import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.PersonChatMessage;
import com.fancy.mobileschool.ui.widget.RoundImageView;

import java.util.List;

/**
 * Created by Jackie on 2017/10/22.
 */

public class ChatPersonMessageAdapter extends RecyclerView.Adapter<ChatPersonMessageAdapter.ChatMessageItem> {

    private final int ITEM_TYPE_ONE = 1;

    private final int ITEM_TYPE_TWO = 2;

    private Context context;

    private List<PersonChatMessage> messages;

    public ChatPersonMessageAdapter(Context context, List<PersonChatMessage> messages) {
        this.messages = messages;
        this.context = context;
    }

    @Override
    public ChatMessageItem onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == ITEM_TYPE_ONE)
            view = LayoutInflater.from(context).inflate(R.layout.chat_right_message_item, parent, false);
        else
            view = LayoutInflater.from(context).inflate(R.layout.chat_left_message_item, parent, false);

        return new ChatMessageItem(view);
    }


    @Override
    public void onBindViewHolder(ChatMessageItem holder, int position) {
        holder.getTextView().setText(messages.get(position).content);
    }


    @Override
    public int getItemCount() {
        return messages.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(messages.get(position).person_from_id.equals(MyConfiguration.myID)){
            return  ITEM_TYPE_ONE;
        }else{
            return ITEM_TYPE_TWO;
        }
    }

    public void addMessage(PersonChatMessage personChatMessage) {
        messages.add(personChatMessage);
    }

    class ChatMessageItem extends RecyclerView.ViewHolder {

        private TextView textView;

        private RoundImageView roundImageView;

        public ChatMessageItem(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.chat_room_message_text);
            roundImageView = (RoundImageView) itemView.findViewById(R.id.chat_room_message_img);
        }

        public TextView getTextView() {
            return textView;
        }

        public RoundImageView getRoundImageView() {
            return roundImageView;
        }
    }
}
